

#ifndef _T_LIST_H_
#define _T_LIST_H_


#include "t_types.h"

#ifdef __cplusplus
extern "C" {
#endif




#define T_OFFSET_OF(type,member)	\
		((T_ADDR)(&(((type *)0)->member)))
#define T_CONTAINER_OF(ptr, type, member) ({			\
        const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
		(type *)( (char *)__mptr - T_OFFSET_OF(type,member) );})


#define T_LIST_ENTRY(ptr, type, member) \
		T_CONTAINER_OF(ptr, type, member)


#define T_LIST_FOR_EACH(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)



#define LIST_HEAD_INIT(name) { &(name), &(name) }


#define LIST_HEAD(name) \
	struct T_LIST_HEAD name = LIST_HEAD_INIT(name)


#define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } while (0)

struct T_LIST_HEAD
{
	struct T_LIST_HEAD *next;
	struct T_LIST_HEAD *prev;
};


static inline T_VOID T_ListAdd(struct T_LIST_HEAD *pNew,
				  struct T_LIST_HEAD *pPrev,
				  struct T_LIST_HEAD *pNext)
{
	pNext->prev = pNew;
	pNew->next = pNext;
	pNew->prev = pPrev;
	pPrev->next = pNew;
}



static inline T_VOID T_ListAddTail(struct T_LIST_HEAD *listNew,struct T_LIST_HEAD *listHead)
{
	T_ListAdd(listNew,listHead->prev,listHead);
}


static inline void __list_del(struct T_LIST_HEAD * prev, struct T_LIST_HEAD * next)
{
	next->prev = prev;
	prev->next = next;
}


static inline T_VOID T_ListDel(struct T_LIST_HEAD *listEntry)
{
	__list_del(listEntry->prev, listEntry->next);
	listEntry->prev = NULL;
	listEntry->next = NULL;

//	listEntry->next->prev = listEntry->prev;
//	listEntry->prev->next = listEntry->next;
//	listEntry->next = NULL;
//	listEntry->prev = NULL;
}




static inline T_BOOL T_ListIsEmpty(struct T_LIST_HEAD *listHead)
{
	return (listHead->next == listHead);
}


static inline int T_ListDump(const T_CHAR *pcString,struct T_LIST_HEAD *listHead)
{
	struct T_LIST_HEAD *pNode;

	printf("%s:",pcString);

	T_LIST_FOR_EACH(pNode,listHead)
	{
		printf("%p->",pNode);
		
	}
	printf("\n");
	return 0;
}

#ifdef __cplusplus
}
#endif

#endif


